// 1. 引入express
const express = require('express');
const {singers} = require('./singers.json');
// console.log('singers',singers)
// 2. 创建应用程序
const app = express();

// 3. 创建路由
app.get('/singer/:id',(req,res)=> {
    console.log(req.params.id)
    let {id} = req.params
    let result = singers.find(item=> {
        if(item.id === Number(id)){
            return true
        }
    })
    if(!result){
        res.statusCode = 404 ;
        res.end('not found');
        return 
    }
    res.end(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <h2>${result.name}</h2>
    </body>
    </html>
    `)
})

// 监听端口，启动服务
app.listen(3000,()=> {
    console.log('express server is running in 3000...')
})